package com.gjs.management.controller;

import com.gjs.common.exception.BizException;
import com.gjs.common.manager.FileValidation;
import com.gjs.common.manager.response.RspBody;
import com.gjs.common.manager.storage.AliyunOss;
import com.gjs.common.pojo.dto.upload.UploadRuleDTO;
import com.gjs.management.pojo.entity.Agreement;
import com.gjs.management.service.IAgreementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(tags = "公共接口")
@RequiredArgsConstructor
@RequestMapping("/common")
public class CommonController {

    private final IAgreementService agreementService;

    private final AliyunOss aliyunOss;

    @GetMapping("/get_obj_storage_domain")
    @ApiOperation("SSO访问地址")
    public RspBody<String> getObjStorageDomain(){
        return RspBody.success(aliyunOss.getHost());
    }

    @GetMapping("/get_agreement")
    @ApiOperation("获取协议内容")
    public RspBody<String> getAgreement(String agreementConfigKey){
        Agreement agreement = agreementService.getAgreementByConfigKey(agreementConfigKey);
        return RspBody.success(agreement.getContent());
    }

    @GetMapping("/get_upload_policy")
    @ApiOperation("获取OSS直传签名")
    public RspBody<Map<String,String>> getUploadPolicy(String contentType,Integer size) {
        Map<String,String> res;
        try {
            // 对文件类型及大小进行校验
            FileValidation.check(contentType,size);

            res = aliyunOss.getUploadPolicy();
        }catch (Exception e){
            throw new BizException(e.getMessage());
        }
        return RspBody.success(res);
    }

    @GetMapping("/get_upload_rule")
    @ApiOperation("获取上传规则")
    public RspBody<List<UploadRuleDTO>> getUploadRule(){
        return RspBody.success(FileValidation.toList());
    }

//
//    @ApiOperation("上传附件")
//    @PostMapping(value = "/upload_file")
//    public RspBody uploadFile(@RequestParam("file") @ApiParam(value = "二进制文件流") MultipartFile file) throws IOException {
//        String fileName = file.getOriginalFilename();//获取文件名
//        fileName = getFileName(fileName);
////        String filepath = getUploadPath();
//        String filepath = "\\\\10.6.100.68\\技术开发部\\技术测试勿删";
//        if (!file.isEmpty()) {
//            try (BufferedOutputStream out = new BufferedOutputStream(
//                    new FileOutputStream(new File(filepath + File.separator + fileName)))) {
//                out.write(file.getBytes());
//                out.flush();
//                return RspBody.success(fileName);
//            } catch (FileNotFoundException e) {
//                return RspBody.error("上传文件失败 FileNotFoundException：" + e.getMessage());
//            } catch (IOException e) {
//                return RspBody.error("上传文件失败 IOException：" + e.getMessage());
//            }
//        } else {
//            return RspBody.error("上传文件失败，文件为空");
//        }
//    }
//
//    /**
//     * 文件名后缀前添加一个时间戳
//     */
//    private String getFileName(String fileName) {
//        int slashIndex = fileName.lastIndexOf("/");
//        final SimpleDateFormat sDateFormate = new SimpleDateFormat("yyyymmddHHmmss");  //设置时间格式
//        String nowTimeStr = sDateFormate.format(new Date()); // 当前时间
//        String fileNameWithNoSlash = fileName.substring(slashIndex);
//        int index = fileNameWithNoSlash.lastIndexOf(".");
//        String ext = fileNameWithNoSlash.substring(index);
//        fileName = fileNameWithNoSlash.substring(0,index) + "_" + nowTimeStr + ext;
//        return fileName;
//    }
//
//    /**
//     * 获取当前系统路径
//     */
//    private String getUploadPath() {
//        File path = null;
//        try {
//            path = new File(ResourceUtils.getURL("classpath:").getPath());
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        if (!path.exists()) path = new File("");
//        File upload = new File(path.getAbsolutePath(), "static/upload/");
//        if (!upload.exists()) upload.mkdirs();
//        return upload.getAbsolutePath();
//    }
}
